package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderListService;
import com.itheima.service.OrderServie;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/orderList")
public class OrderListController {

    @Reference
    private OrderServie orderServie;

    @Reference
    private OrderListService orderListService;

    /**
     * 添加预约
     * @param setmealId
     * @param addressId
     * @param map
     * @return
     */
    @RequestMapping("add")
    public Result add(Integer setmealId, Integer addressId, @RequestBody Map map){
        Result order = null;

        try {
            map.put("setmealId",setmealId.toString());
            map.put("addressId",addressId.toString());
           order = orderServie.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return order ;
        }
        if (order.isFlag()) {
            try {
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, (String) map.get("telephone"), (String) map.get("orderDate"));
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return order;
    }

    /**
     * 分页查询预约列表
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderListService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 修改预约状态--1
     * @param id
     * @return
     */
    @RequestMapping("update1")
    public Result update1(Integer id){
        String status = "已到诊";
        Result result = orderListService.update(id,status);
        return result;
    }

    /**
     * 修改预约状态--2
     * @param id
     * @return
     */
    @RequestMapping("update2")
    public Result update2(Integer id){
        String status = "已取消";
        Result result = orderListService.update(id,status);
        return result;
    }
}
